package com.example.pc.videodemo.emq

import android.util.Log
import com.example.myapplication.emq.model.verify_weigh.OnLinePublisher
import com.example.myapplication.emq.model.verify_weigh.OnlineSubscriber
import com.example.myapplication.rabbitmq.OnReceivedDataListener
import com.example.myapplication.rabbitmq.OnReceivedMsgListener
import com.example.pc.videodemo.emq.model.MqttMassgeSubscribBase
import com.example.pc.videodemo.emq.model.answer.AnswerPublisher
import com.example.pc.videodemo.emq.model.answer.AnswerSubscriber
import com.example.pc.videodemo.emq.model.call.CallPublisher
import com.example.pc.videodemo.emq.model.call.CallSubscriber
import com.example.pc.videodemo.emq.model.deviceData.DeviceDataPublisher
import com.example.pc.videodemo.emq.model.hangUp.HangupPublisher
import com.example.pc.videodemo.emq.model.hangUp.HangupSubscriber
import com.example.pc.videodemo.emq.model.incall.InCallPublisher
import com.example.pc.videodemo.emq.model.incall.InCallSubscriber
import com.example.pc.videodemo.emq.model.ip_address.IpAddressPublisher
import com.example.pc.videodemo.emq.model.ip_address.IpAddressSubscriber
import com.example.pc.videodemo.emq.model.locationPhone.LocationPhonePublisher
import com.example.pc.videodemo.emq.model.locationPhone.LocationPhoneSubscriber
import com.example.pc.videodemo.emq.model.occupy.OccupyPublisher
import com.example.pc.videodemo.emq.model.occupy.OccupySubscriber
import com.example.pc.videodemo.emq.model.offline.OffLinePublisher
import com.example.pc.videodemo.emq.model.offline.OfflineSubscriber


class MqttManager private constructor() : OnReceivedMsgListener {
    private var sn = ""
    private var mqttHelper: MqttHelper?= null
    private val subscriberList: MutableList<Subscriber>
    private var mqttMessageService: MqttMessageService = MqttMessageService()
    private var onReceivedMsgListenerList: ArrayList<OnReceivedDataListener?> = ArrayList()
    fun getSn(): String {
        return sn
    }

    fun setOnReceivedDataListener(onReceivedMsgListener: OnReceivedDataListener?) {
        if (!onReceivedMsgListenerList.contains(onReceivedMsgListener))
            this.onReceivedMsgListenerList.add(onReceivedMsgListener)
    }

    fun removeListener(onReceivedMsgListener: OnReceivedDataListener?) {
        onReceivedMsgListenerList.remove(onReceivedMsgListener)
    }

    fun setSn(sn: String) {
        this.sn = sn

    }

    fun start() {
        mqttMessageService.setOnReceivedMsgListener(this)
        mqttHelper = MqttHelper(mqttMessageService)
        mqttMessageService.initMqtt(sn)
        mqttMessageService.connectMqtt()
    }

    fun disconnect() {
        mqttMessageService.disConnect()
    }

    init {

        subscriberList = ArrayList()
        subscriberList.add(OnlineSubscriber())
        subscriberList.add(OfflineSubscriber())
        subscriberList.add(OccupySubscriber())
        subscriberList.add(HangupSubscriber())
        subscriberList.add(CallSubscriber())
        subscriberList.add(IpAddressSubscriber())
        subscriberList.add(InCallSubscriber())
        subscriberList.add(AnswerSubscriber())
        subscriberList.add(LocationPhoneSubscriber())
    }

    fun answer(s: String?) {
        val answerPublisher = s?.let { AnswerPublisher(it) }
        answerPublisher?.setMqttHelper(mqttHelper)
        answerPublisher?.sendMessage(sn)
    }

    fun location(s: String?) {
        val locationPhonePublisher = s?.let { LocationPhonePublisher(it) }
        locationPhonePublisher?.setMqttHelper(mqttHelper)
        locationPhonePublisher?.sendMessage(sn)
    }

    fun callIn(s: String?) {
        val inCallPublisher = s?.let { InCallPublisher(it) }
        inCallPublisher?.setMqttHelper(mqttHelper)
        inCallPublisher?.sendMessage(sn)
    }

    fun sendIp(s: String?) {
        val ipAddressPublisher = IpAddressPublisher(s)
        ipAddressPublisher.setMqttHelper(mqttHelper)
        ipAddressPublisher.sendMessage(sn)
    }

    fun online() {
        val poster = OnLinePublisher()
        poster.setMqttHelper(mqttHelper)
        poster.sendMessage(sn)
    }

    fun offline() {
        val poster = OffLinePublisher()
        poster.setMqttHelper(mqttHelper)
        poster.sendMessage(sn)
    }

    fun call(text: String) {
        val poster = CallPublisher(text)
        poster.setMqttHelper(mqttHelper)
        poster.sendMessage(sn)
    }

    fun hangup(toString: String) {
        val poster = HangupPublisher(toString)
        poster.setMqttHelper(mqttHelper)
        poster.sendMessage(sn)
    }

    fun occupy() {
        val poster = OccupyPublisher()
        poster.setMqttHelper(mqttHelper)
        poster.sendMessage(sn)
    }

    fun deviceData(text: String, phoneType: String) {
        val poster = DeviceDataPublisher(text, phoneType)
        poster.setMqttHelper(mqttHelper)
        poster.sendMessage(sn)
    }


    override fun onReceived(contents: String) {

        for (subscriber in subscriberList) {
            if (contents.contains(subscriber.emq.code)) {
                subscriber.onReceived(contents)
                break
            }
        }
    }

    override fun onConnect() {
        onReceivedMsgListenerList?.get(onReceivedMsgListenerList.size - 1)?.onMqConnect()

    }

    override fun onError() {
        onReceivedMsgListenerList?.get(onReceivedMsgListenerList.size - 1)?.onError()
        mqttMessageService.disConnect()
        mqttMessageService = MqttMessageService()
        start()
    }

    fun onReceivedData(receiveBean: MqttMassgeSubscribBase) {
        Log.i("subscriber", "onReceived: " + receiveBean.toString())

        onReceivedMsgListenerList?.get(onReceivedMsgListenerList.size - 1)?.onReceivedData(receiveBean)
    }

    companion object {
        var instance: MqttManager? = null
            get() {
                if (field == null) field = MqttManager()
                return field
            }
            private set
    }
}